// 全局的token、用户信息这些内容存储的信息使用cookie还是
/* 这里是获取用户token的，用户成功登录后保存用户的token值，用于路由的拦截 */

import cookie from 'js-cookie'
import router from '../router'
import store from '@/store/index.js'

const token = 'token' //直接抽取取来相同的名称
const userName = 'username'
const account = 'account' //账户
const password = 'password' //密码

/* 设置、获取和移出token值 */
export function getToken() {
  return cookie.get(token)
}

export function setToken(value) {
  return cookie.set(token, value)
}
/*
1、用户手动退出账号的时候清除token
2、用户从后台首页中退出到login页面的时候，要清除token、用户信息
*/
export function removeToken(value) {
  return cookie.remove(token, token)
}
// 设置、获取。清除用户名
export function setUserName(value) {
  return cookie.set(userName, value)
}
export function getUserName() {
  return cookie.get(userName)
}
export function removeUserName() {
  return cookie.remove(userName)
}

// 设置账户
export function setAccount(value) {
  return cookie.remove(account, value)
}
export function getAccount() {
  return cookie.get(account)
}
export function removeAccount() {
  return cookie.remove(account)
}
// 设置密码
export function setPassword(value) {
  return cookie.remove(password, value)
}
export function getPassword() {
  return cookie.get(password)
}
export function removePassword() {
  return cookie.remove(password)
}

// 过滤异步路由表
export function filterAsyncRoutes(arrPermissions, asyncRoutes) {
  return asyncRoutes.filter(route => {
    const associated = arrPermissions.find(permission => route.meta.title === permission.name)

    if (!associated) {
      return false
    }
    // console.log('associated',associated);
    if (associated.operationPermissions === '0' && route.meta.type === 'action') return false
    // console.log('route',route.meta.title);
    if (!associated?.accessPermissions || associated.operationPermissions === '1' || associated?.accessPermissions === '1') {
      if (route.children && associated?.children) {
        route.children = filterAsyncRoutes(associated.children, route.children)
      }
      // console.log(route.meta.type);
      return true
    }
  })
}

// 修正redirect
export const correctRedirect = routes => {
  routes.forEach(route => {
    if (route.children?.length > 0 && route.redirect) {
      // console.log(route.redirect);
      const newRedirect = route.children[0].path || route.children.find(item => item.children?.length > 0)?.path

      if (newRedirect) {
        route.redirect = newRedirect
      }
      correctRedirect(route.children)
    }
  })
}

// 排除空children
export function excludeEmpty(data) {
  for (let i = 0; i < data.length; i++) {
    const node = data[i]
    if (node.children) {
      excludeEmpty(node.children)
      if (node.children.length === 0) {
        data.splice(i, 1)
        i--
      }
    }
  }
  return data
}
//递归，解决展示问题
export function clear(data) {
  for (let index = data.length - 1; index >= 0; index--) {
    if (data[index]['children']) {
      clear(data[index]['children'])
      if (data[index]['children']['length'] === 0) {
        data.splice(index, 1)
      }
    }
  }
  return data
}

export function exitLogin(data) {
  store.dispatch('logOut')
  router.push({
    path: '/login'
  })
  // 移除路由
  store.state.newAsyncRoutes.forEach(item => router.removeRoute(item.name))
  store.commit('resetNewAsyncRoutes')
}
